package com.supermap.business.report;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.china.StatisticsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author:zhoujian
 * @Desciption:报表
 * @date:2019-04-29 17:47
 * @version:v1.0.0
 */
@RestController
@RequestMapping("report")
public class ReportController {

    @Reference(timeout = 10000)
    StatisticsService statisticsService;

    @GetMapping("statisticsByRangDate")
    public JSONUtils statistics(String areaCode , String startDate , String endDate)
    {
        //新增数量
        JSONUtils addJSON = statisticsService.syntheticalStatistics(areaCode,startDate,endDate);

        List<Map<String , Object>> addList = (List<Map<String , Object>> )addJSON.getData();
        //总数数量
        JSONUtils json = statisticsService.syntheticalStatistics(areaCode,null,null);
        List<Map<String , Object>> totalList = (List<Map<String , Object>> )json.getData();
        //公众服务产品新增
        JSONUtils publicAddJson = statisticsService.publicProductStatistics(areaCode, startDate, endDate);
        List<Map<String , Object>> publicAddList = (List<Map<String , Object>> )publicAddJson.getData();
        //公众服务产品总数
        JSONUtils publicTotalJson = statisticsService.publicProductStatistics(areaCode, null, null);
        List<Map<String , Object>> publicTotalList = (List<Map<String , Object>> )publicTotalJson.getData();


        LinkedHashMap<String , Map<String , Object>> tmpMap = new LinkedHashMap<String , Map<String , Object>>();

        for ( Map<String , Object> map: addList) {
            String key = map.get("areaname").toString();
            if (tmpMap.containsKey(key))
            {
                //存在
            }else{
                //不存在
                Map<String,Object> tempMap1 = new HashMap<String,Object>();
                tempMap1.putAll(map);
                tmpMap.put(key,tempMap1);
            }
        }

        for ( Map<String , Object> map: publicAddList) {
            String key = map.get("areaname").toString();
            if (tmpMap.containsKey(key))
            {
                //存在
                tmpMap.get(key).putAll(map);
            }else{
                //不存在
                Map<String,Object> tempMap1 = new HashMap<String,Object>();
                tempMap1.putAll(map);
                tmpMap.put(key,tempMap1);
            }
        }

        for ( Map<String , Object> map: totalList) {
            String key = map.get("areaname").toString();
            if (tmpMap.containsKey(key))
            {
                String surfix = "total_";
                for (String k: map.keySet()) {
                    if(!"areaname".equals(k.toString()) && !"active_expert_count".equals(k.toString()))
                    {
                        String newKey = surfix + k;
                        tmpMap.get(key).put(newKey,map.get(k));
                    }
                }
            }else{
                //不存在
                Map<String,Object> tempMap1 = new HashMap<String,Object>();
                tempMap1.putAll(map);
                tmpMap.put(key,tempMap1);
            }
        }

        for ( Map<String , Object> map: publicTotalList) {
            String key = map.get("areaname").toString();
            if (tmpMap.containsKey(key))
            {
                String surfix = "total_";
                for (String k: map.keySet()) {
                    if(!"areaname".equals(k.toString()) && !"active_expert_count".equals(k.toString()))
                    {
                        String newKey = surfix + k;
                        tmpMap.get(key).put(newKey,map.get(k));
                    }
                }
            }else{
                //不存在
                Map<String,Object> tempMap1 = new HashMap<String,Object>();
                tempMap1.putAll(map);
                tmpMap.put(key,tempMap1);
            }
        }

        List resultList = new ArrayList();
        for (String key: tmpMap.keySet()) {
            resultList.add(tmpMap.get(key));
        }
        return  new JSONUtils().setData(resultList).setCountSum(resultList.size());
    }
}